{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<bound method PageElement.get_text of <table class=\"table table-striped table-data\">\n",
      "<tr>\n",
      "<th>ACCEPT</th>\n",
      "<td>text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>ACCEPT-ENCODING</th>\n",
      "<td>gzip, deflate, br</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>CONNECTION</th>\n",
      "<td>keep-alive</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>CONTENT-LENGTH</th>\n",
      "<td></td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>CONTENT-TYPE</th>\n",
      "<td></td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>HOST</th>\n",
      "<td>www.whatismybrowser.com</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>USER-AGENT</th>\n",
      "<td>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36</td>\n",
      "</tr>\n",
      "</table>>\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "session = requests.Session()\n",
    "headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',\n",
    "           'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7'}\n",
    "url = 'https://www.whatismybrowser.com/'\\\n",
    "    'developers/what-http-headers-is-my-browser-sending'\n",
    "req = session.get(url, headers=headers)\n",
    "\n",
    "bs = BeautifulSoup(req.text, 'html.parser')\n",
    "print(bs.find('table', {'class':'table-striped'}).get_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from webdriver_manager.chrome import ChromeDriverManager\n",
    "CHROMEDRIVER_PATH = ChromeDriverManager().install()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'domain': '.pythonscraping.com', 'expiry': 1722996491, 'httpOnly': False, 'name': '_ga', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GA1.1.285394841.1688436491'}, {'domain': '.pythonscraping.com', 'expiry': 1722996491, 'httpOnly': False, 'name': '_ga_G60J5CGY1N', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GS1.1.1688436491.1.0.1688436491.0.0.0'}]\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "\n",
    "chrome_options = Options()\n",
    "chrome_options.add_argument(\"--headless\")\n",
    "driver = webdriver.Chrome(service=Service(CHROMEDRIVER_PATH), options=chrome_options)\n",
    "\n",
    "driver.get('http://pythonscraping.com')\n",
    "driver.implicitly_wait(1)\n",
    "print(driver.get_cookies())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'domain': '.pythonscraping.com', 'expiry': 1722997590, 'httpOnly': False, 'name': '_ga', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GA1.1.678086850.1688437590'}, {'domain': '.pythonscraping.com', 'expiry': 1722997590, 'httpOnly': False, 'name': '_ga_G60J5CGY1N', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GS1.1.1688437589.1.0.1688437589.0.0.0'}]\n",
      "[{'domain': '.pythonscraping.com', 'expiry': 1722997597, 'httpOnly': False, 'name': '_ga_G60J5CGY1N', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GS1.1.1688437589.1.1.1688437597.0.0.0'}, {'domain': '.pythonscraping.com', 'expiry': 1722997597, 'httpOnly': False, 'name': '_ga', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GA1.1.678086850.1688437590'}]\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "\n",
    "chrome_options = Options()\n",
    "chrome_options.add_argument(\"--headless\")\n",
    "driver = webdriver.Chrome(service=Service(CHROMEDRIVER_PATH), options=chrome_options)\n",
    "\n",
    "driver.get('http://pythonscraping.com')\n",
    "driver.implicitly_wait(1)\n",
    "\n",
    "savedCookies = driver.get_cookies()\n",
    "print(savedCookies)\n",
    "\n",
    "driver2 = webdriver.Chrome(service=Service(CHROMEDRIVER_PATH), options=chrome_options)\n",
    "\n",
    "driver2.get('http://pythonscraping.com')\n",
    "driver2.delete_all_cookies()\n",
    "for cookie in savedCookies:\n",
    "    driver2.add_cookie(cookie)\n",
    "\n",
    "driver2.get('http://pythonscraping.com')\n",
    "driver.implicitly_wait(1)\n",
    "print(driver2.get_cookies())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The link http://pythonscraping.com/dontgohere is a trap\n",
      "Do not change value of phone\n",
      "Do not change value of email\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.remote.webelement import WebElement\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "driver = webdriver.Chrome(service=Service(CHROMEDRIVER_PATH))\n",
    "\n",
    "driver.get('http://pythonscraping.com/pages/itsatrap.html')\n",
    "links = driver.find_elements(By.TAG_NAME, 'a')\n",
    "for link in links:\n",
    "    if not link.is_displayed():\n",
    "        print(f'The link {link.get_attribute(\"href\")} is a trap')\n",
    "\n",
    "fields = driver.find_elements(By.TAG_NAME, 'input')\n",
    "for field in fields:\n",
    "    if not field.is_displayed():\n",
    "        print(f'Do not change value of {field.get_attribute(\"name\")}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
